﻿// -----------------------------------------------------------------------
// <copyright file="NeedsDisplay.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Cubezicles
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    /// <summary>
    /// Manages the display of the needs for the currently selected employee
    /// </summary>
    public class NeedsDisplay : DrawableGameComponent
    {
        // Texture that we'll stretch out and re-color to display the NeedsMeter.
        private Texture2D dot;

        //Texture for the interface background and mood
        private Texture2D bg, mood;


        // - Parameters for drawing lines of text
        private const float StatusX = 470.0f; //Might want to rewrite this in terms of window width

        // Distance from left side of the screen where the meter starts
        private const float MeterX = 560.0f;

        // Distance from the top of each line where the meter starts
        private const float MeterOffset = 6.0f;

        // Height of each meter
        private const float MeterHeight = 12.0f;
        
        // the height of a line in pixels
        private const int LineHeight = 20;

        // Current Employee format
        const string employeeFormat = "Current Employee: {0}";

        // Get the active employee from the Game1
        private Employee emp { get { return game1.CurrentEmployee; }}

        // Get the Game1 object stored by the base class
        private Game1 game1 { get { return (Game1)Game; } }

        public NeedsDisplay(Game1 game) : base(game) { }

        protected override void LoadContent()
        {
            dot = Game.Content.Load<Texture2D>("Textures/dot");
            bg = Game.Content.Load<Texture2D>("UI/workerstatbg");
            mood = Game.Content.Load<Texture2D>("UI/mood");
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            //Get the screen width and height
            int screenH = GraphicsDevice.Viewport.Height;
            int screenW = GraphicsDevice.Viewport.Width;

            // If no employee is selected, this component does nothing
            if (emp == null)
            {
                return;
            }

            game1.SpriteBatch.Begin();

            // The line (from top of screen) to start writing at
            float currY = screenH - 160;

            //Draw the background and Avatar's mood
            game1.SpriteBatch.Draw(bg, new Vector2(screenW - bg.Width, currY), Color.White);
            //We should proooooooooooobably change the color of the mood once we figure out who we want to determine the mood.
            game1.SpriteBatch.Draw(mood, new Vector2(screenW - mood.Width, currY-5), Color.Green);

            // Display the employee's name
            //var employeeMsg = string.Format(employeeFormat, game1.CurrentEmployee.Name);
            //game1.SpriteBatch.DrawString(game1.Fonts["status"], employeeMsg,
                //new Vector2(screenW/2, screenH - 120), Color.Gainsboro);

            //currY += LineHeight;

            //var statusString = "Status: " + emp.GetStateString();
           // game1.SpriteBatch.DrawString(game1.Fonts["status"], statusString,
            //    new Vector2(StatusX, currY), Color.BlueViolet);

            //currY += LineHeight;
            currY = DrawNeed(emp.MoodLevels.hunger, "Hunger", currY);
            currY = DrawNeed(emp.MoodLevels.fun, "Fun", currY);
            currY = DrawNeed(emp.MoodLevels.energy, "Energy", currY);
            currY = DrawNeed(emp.MoodLevels.bladder, "Bladder", currY);
            currY = DrawNeed(emp.MoodLevels.focus, "Focus", currY);
            game1.SpriteBatch.End();
 	        base.Draw(gameTime);
        }

        /// <summary>
        /// Draw the name and meter for a particular need.
        /// If the need is still at a "good" level, will draw meter in green, otherwise meter will be red.
        /// </summary>
        /// <param name="need">the need float variable</param>
        /// <param name="needName">Name of the need (to write to the screen)</param>
        /// <param name="y">Y position of cursor</param>
        /// <returns>New Y position for cursor</returns>
        private float DrawNeed(float need, string needName, float y)
        {
            var statNameStart = new Vector2(StatusX, y);

            // Rectangle outlining the health meter.
            var meterRect = new Rectangle((int)MeterX, (int)(y + MeterOffset),
                                          (int)(100 * need), (int)MeterHeight);

            // Show needs in red if their levels are "critical"
            var color = need > 0.2 ? Color.Green : Color.Red;

            game1.SpriteBatch.DrawString(game1.Fonts["needs"], needName + ": ", statNameStart, color);
            game1.SpriteBatch.Draw(dot, meterRect, color);

            return y + LineHeight;
        }
    }
}
